package fi.pakkanen.yarpg.item;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import fi.pakkanen.yarpg.item.trait.Consumable;

/**
 * An item with a limited amount of charges. After it is used and there are no
 * charges left it is consumed and cannot be triggered anymore.
 * 
 * @author Oliver Michels <oliver.michels@gmx.net>
 * 
 */
public class ConsumableItem extends TriggerableItem implements Consumable {

	/** The valid states for this item */
	private static Set<ItemState> validStates;

	static {
		ItemState[] valids = { ItemState.INACTIVE, ItemState.ACTIVE, ItemState.RECHARGING, ItemState.CONSUMED };
		validStates = new HashSet<ItemState>(Arrays.asList(valids));
	}

	/** The remaining charges */
	private int remainingCharges;

	/**
	 * Create a consumable item that is consumed after it is used.
	 */
	public ConsumableItem() {
		this(0, 0, 1);
	}

	/**
	 * Create a consumable item with the given duration time and recharging
	 * time.
	 * 
	 * @param durationTimeInSeconds
	 *            the duration in seconds
	 * @param rechargeTimeInSeconds
	 *            the time to re-charge in seconds
	 */
	public ConsumableItem(final int durationTimeInSeconds, final int rechargeTimeInSeconds) {
		this(durationTimeInSeconds, rechargeTimeInSeconds, 1);
	}

	/**
	 * Create a consumable item with the given amount of charges, a given
	 * duration and time to re-charge.
	 * 
	 * @param durationTimeInSeconds
	 *            the duration in seconds
	 * @param rechargeTimeInSeconds
	 *            the time to re-charge in seconds
	 * @param charges
	 *            the amount of charges before it is consumed
	 */
	public ConsumableItem(final int durationTimeInSeconds, final int rechargeTimeInSeconds, final int charges) {
		super(durationTimeInSeconds, rechargeTimeInSeconds);

		remainingCharges = charges;
	}

	@Override
	public boolean isConsumable() {
		return true;
	}

	@Override
	public int reduceCharges() {
		return --remainingCharges;
	}

	@Override
	public int getRemainingCharges() {
		return remainingCharges;
	}

	@Override
	public boolean isConsumed() {
		return remainingCharges == 0;
	}

	@Override
	protected Set<ItemState> getValidStates() {
		return validStates;
	}

}
